//
//  QYXDailyDetailCollectionViewCell.m
//  OpenYourEyes
//
//  Created by dllo on 16/6/7.
//  Copyright © 2016年 Google AdWords. All rights reserved.
//

#import "QYXDailyDetailCollectionViewCell.h"
#import "QYXDailyTagsModel.h"
#import "UIImageView+QYXCarveImage.h"

@implementation QYXDailyDetailCollectionViewCell

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        /** 播放按钮的点击手势 */
        UITapGestureRecognizer *playTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapPlayImage:)];
        UITapGestureRecognizer *playImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapPlayImage:)];
        /** 分享按钮的点击手势 */
        UITapGestureRecognizer *shareTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedShareLabel:)];
        UITapGestureRecognizer *shareImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedShareLabel:)];
        /** 评论按钮的点击手势 */
        UITapGestureRecognizer *talkTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapTalkLabel:)];
        UITapGestureRecognizer *talkImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapTalkLabel:)];
        /** 收藏按钮的点击手势 */
        UITapGestureRecognizer *heartTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapHeartLabel:)];
        UITapGestureRecognizer *heartImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapHeartLabel:)];
        /** 下载按钮的点击手势 */
        UITapGestureRecognizer *downloadTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapDownload:)];
        UITapGestureRecognizer *downloadImageTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(didClickedTapDownload:)];

        
        
        
        //初始化背景图片
        self.backImageView = [[UIImageView alloc] initWithFrame:self.contentView.bounds];
        self.backImageView.userInteractionEnabled = YES;
        [self.contentView addSubview:_backImageView];
        
      
        //初始化暗面背景视图
        self.blackView = [[UIView alloc] initWithFrame:self.backImageView.bounds];
        self.blackView.backgroundColor = [UIColor blackColor];
        self.blackView.alpha = 0.5;
        self.blackView.userInteractionEnabled = YES;
        [self.backImageView addSubview:_blackView];
        
      
        //初始化主要照片:
        self.mainImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.contentView.frame.size.width, (self.contentView.frame.size.height - 64 - 49) / 3 * 2)];
        self.mainImageView.userInteractionEnabled = YES;
        [self.mainImageView addGestureRecognizer:playImageTap];
        [self.backImageView addSubview:_mainImageView];
        [UIImageView CareImageWithUIImageView:_mainImageView];
      
        //初始化title
        self.titleLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.mainImageView.frame.size.height + 20, self.mainImageView.frame.size.width - 20, 20)];
        self.titleLabel.font = [UIFont fontWithName:@"FZLTZCHJW--GB1-0" size:17];
        self.titleLabel.textColor = [UIColor whiteColor];
        [self.backImageView addSubview:_titleLabel];
        
       
        //初始化line
        self.lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.titleLabel.frame.size.height + self.titleLabel.frame.origin.y + 19, self.titleLabel.frame.size.width / 2, 1)];
        self.lineLabel.backgroundColor = [UIColor whiteColor];
        [self.backImageView addSubview:_lineLabel];
        
       
        //初始化time
        self.timeLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.lineLabel.frame.size.height + self.lineLabel.frame.origin.y + 19, 70, 15)];
        self.timeLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
        self.timeLabel.textColor = [UIColor lightGrayColor];
        [self.backImageView addSubview:_timeLabel];
        
        
        //初始化detail
        self.detailLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, self.timeLabel.frame.size.height + self.timeLabel.frame.origin.y + 20, self.titleLabel.frame.size.width, 80)];
        self.detailLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:15];
        self.detailLabel.numberOfLines = 0;
        self.detailLabel.textColor = [UIColor whiteColor];
        [self.backImageView addSubview:_detailLabel];
        
        
        //初始化360
        self.fullViewLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 10, self.contentView.frame.size.width - 10, 20)];
        self.fullViewLabel.textAlignment = 2;
        self.fullViewLabel.textColor = [UIColor whiteColor];
        self.fullViewLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:15];
        [self.backImageView addSubview:_fullViewLabel];
        
        
        //初始化heart
        self.collectionCount = [[UILabel alloc] initWithFrame:CGRectMake(10 + 20 + 5, self.detailLabel.frame.size.height + self.detailLabel.frame.origin.y + 30, 65, 20)];
        self.collectionCount.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
        self.collectionCount.textColor = [UIColor whiteColor];
        self.collectionCount.userInteractionEnabled = YES;
        [self.collectionCount addGestureRecognizer:heartTap];
        [self.backImageView addSubview:_collectionCount];
        
        self.heartImageView = [[UIImageView alloc] initWithFrame:CGRectMake(10, self.detailLabel.frame.size.height + self.detailLabel.frame.origin.y + 30, 20, 20)];
        self.heartImageView.userInteractionEnabled = YES;
        [self.heartImageView addGestureRecognizer:heartImageTap];
        [self.backImageView addSubview:_heartImageView];
        
        
        //初始化share
        self.shareCount = [[UILabel alloc] initWithFrame:CGRectMake(self.collectionCount.frame.size.width + self.collectionCount.frame.origin.x + 5 + 20 + 5, self.collectionCount.frame.origin.y, 65, 20)];
        self.shareCount.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
        self.shareCount.textColor = [UIColor whiteColor];
        self.shareCount.userInteractionEnabled = YES;
        [self.shareCount addGestureRecognizer:shareTap];
        [self.backImageView addSubview:_shareCount];
        
        self.shareImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.collectionCount.frame.size.width + self.collectionCount.frame.origin.x + 5, self.collectionCount.frame.origin.y, 20, 20)];
        self.shareImageView.userInteractionEnabled = YES;
        [self.shareImageView addGestureRecognizer:shareImageTap];
        [self.backImageView addSubview:_shareImageView];
        
       
        //初始化评论
        self.playImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.shareCount.frame.origin.x + self.shareCount.frame.size.width + 5, self.shareCount.frame.origin.y, 20, 20)];
        self.playImageView.userInteractionEnabled = YES;
        [self.playImageView addGestureRecognizer:talkImageTap];
        [self.backImageView addSubview:_playImageView];
        
        self.playCount = [[UILabel alloc] initWithFrame:CGRectMake(self.playImageView.frame.origin.x + self.playImageView.frame.size.width + 5, self.playImageView.frame.origin.y, 65, 20)];
        self.playCount.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
        self.playCount.textColor = [UIColor whiteColor];
        self.playCount.userInteractionEnabled = YES;
        [self.playCount addGestureRecognizer:talkTap];
        [self.backImageView addSubview:_playCount];
        
       
        //初始化download
        self.downloadImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.playCount.frame.origin.x + self.playCount.frame.size.width + 5, self.playCount.frame.origin.y, 20, 20)];
        self.downloadImageView.userInteractionEnabled = YES;
        [self.downloadImageView addGestureRecognizer:downloadImageTap];
        [self.backImageView addSubview:_downloadImageView];
        
        self.downloadLabel = [[UILabel alloc] initWithFrame:CGRectMake(self.downloadImageView.frame.size.width + self.downloadImageView.frame.origin.x + 5, self.downloadImageView.frame.origin.y, 65, 20)];
        self.downloadLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:13];
        self.downloadLabel.textColor = [UIColor whiteColor];
        self.downloadLabel.userInteractionEnabled = YES;
        [self.downloadLabel addGestureRecognizer:downloadTap];
        [self.backImageView addSubview:_downloadLabel];
        
       
        //初始化播放按钮
        self.pImageView = [[UIImageView alloc] initWithFrame:CGRectMake(self.contentView.frame.size.width / 2 - 32, self.mainImageView.frame.size.height / 2 - 32, 64, 64)];
        self.pImageView.userInteractionEnabled = YES;
        [self.pImageView addGestureRecognizer:playTap];
        [self.mainImageView addSubview:_pImageView];
        
    }
    return self;
}

#pragma mark -- 播放按钮的点击手势:
- (void)didClickedTapPlayImage:(UITapGestureRecognizer *)tap
{
    if (_delegate != nil && [_delegate respondsToSelector:@selector(pushVCByVideoModel:)]) {
        [_delegate pushVCByVideoModel:_model];
    }
    NSLog(@"播放");
}
#pragma mark -- 分享按钮点击手势方法:
- (void)didClickedShareLabel:(UITapGestureRecognizer *)tap
{
    NSLog(@"分享");
}
#pragma mark -- 评论的点击手势方法:
- (void)didClickedTapTalkLabel:(UITapGestureRecognizer *)tap
{
    NSLog(@"评论");
    [self.delegate presentViewByTalkLabel:_model.id];
}
#pragma mark -- 收藏按钮的点击手势:
- (void)didClickedTapHeartLabel:(UITapGestureRecognizer *)tap
{
    NSLog(@"收藏");
}
#pragma mark -- 下载按钮的点击手势:
- (void)didClickedTapDownload:(UITapGestureRecognizer *)tap
{
    NSLog(@"下载");
}


- (void)setTopicsLabel:(NSArray *)topicsLabel
{
    if (_topicsLabel != topicsLabel) {
        _topicsLabel = topicsLabel;
    }
}

- (void)setModel:(QYXDailyItemListModel *)model
{
    if (_model != model) {
        _model = model;
    }
    [self.backImageView sd_setImageWithURL:[NSURL URLWithString:[_model.cover objectForKey:@"blurred"]] placeholderImage:kPlaceHolderImage];
    [self.mainImageView sd_setImageWithURL:[NSURL URLWithString:[_model.cover objectForKey:@"detail"]] placeholderImage:kPlaceHolderImage];
    self.titleLabel.text = _model.title;
    self.timeLabel.text = [NSString stringWithFormat:@"时长:  %ld'%ld''", _model.duration / 60, _model.duration % 60];
    self.detailLabel.text = _model.des;
    self.collectionCount.text = [NSString stringWithFormat:@"%@", [_model.consumption objectForKey:@"collectionCount"]];
    self.shareCount.text = [NSString stringWithFormat:@"%@", [_model.consumption objectForKey:@"shareCount"]];
    self.playCount.text = [NSString stringWithFormat:@"%@", [_model.consumption objectForKey:@"replyCount"]];
    self.heartImageView.image = [UIImage imageNamed:@"heart_white"];
    self.shareImageView.image = [UIImage imageNamed:@"share_white"];
    self.playImageView.image = [UIImage imageNamed:@"talk_white"];
    self.downloadLabel.text = @"缓存";
    self.downloadImageView.image = [UIImage imageNamed:@"download_white"];
    if (_model.label) {
        self.fullViewLabel.text = [_model.label objectForKey:@"text"];
    }
    self.pImageView.image = [UIImage imageNamed:@"play_back"];
    //数组:

    if (_topicsLabel.count <= 3 && _topicsLabel.count != 0) {
        for (NSInteger i = 0; i < _topicsLabel.count; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(self.timeLabel.frame.origin.x + self.timeLabel.frame.size.width + 10 + 60 * i, self.timeLabel.frame.origin.y, 55, 20);
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:10];
            button.backgroundColor = [UIColor grayColor];
            button.tag = 1000 + i;
            QYXDailyTagsModel *tagModel = [_model.tags objectAtIndex:i];
            [button setTitle:tagModel.name forState:UIControlStateNormal];
            [self.backImageView addSubview:button];
            [button addTarget:self action:@selector(didClickedTapTopicsLabel:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
    if (_topicsLabel.count > 3) {
        for (NSInteger i = 0; i < 3; i++) {
            UIButton *button = [UIButton buttonWithType:UIButtonTypeCustom];
            button.frame = CGRectMake(self.timeLabel.frame.origin.x + self.timeLabel.frame.size.width + 10 + 60 * i, self.timeLabel.frame.origin.y, 55, 20);
            [button setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
            button.titleLabel.font = [UIFont fontWithName:@"FZLTXIHJW--GB1-0" size:10];
            button.backgroundColor = [UIColor grayColor];
            button.tag = 1000 + i;
            QYXDailyTagsModel *tagModel = [_model.tags objectAtIndex:i];
            [button setTitle:tagModel.name forState:UIControlStateNormal];
            [self.backImageView addSubview:button];
            [button addTarget:self action:@selector(didClickedTapTopicsLabel:) forControlEvents:UIControlEventTouchUpInside];
        }
    }
}
#pragma mark -- 标签的点击手势:
- (void)didClickedTapTopicsLabel:(UIButton *)sender
{
    QYXDailyTagsModel *tagModel = [_model.tags objectAtIndex:sender.tag - 1000];
    [self.delegate pushVCByTopListLabel:tagModel.id];
}
@end
